//页面加载完成
$(function () {


    var  emp_datagrid =  $("#emp_datagrid");

    var emp_form =  $("#emp_form");

    var emp_dialog =  $("#emp_dialog");

    var combobox_role =  $("#combobox_role");

    //初始员工列表界面
    emp_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: '/employee/list',
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'id', hidden: true},
            {field: 'userName', title: '名称', width: 100},
            {field: 'realName', title: '真实姓名', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {field: 'email', title: '邮箱', width: 100},
            {field: 'dept', title: '部门', width: 100, formatter: deptFormatter},
            {field: 'inputTime', title: '入职时间', width: 100},
            {field: 'state', title: '状态', width: 100, formatter: stateFormatter},
            {field: 'admin', title: '管理员', width: 100, formatter: adminFormatter},

        ]],
        toolbar: "#tb",
        onClickRow:function(index,row){
            console.log(row);
            if(row.state == 0){

                $("#btn_edit,#btn_quit").linkbutton("enable");
            }else if(row.state == 1){
                $("#btn_edit,#btn_quit").linkbutton("disable");
            }
        }




    })
        //将方法绑定到对象中
        var cmdObj = {

            addEmp:function(){

                //清空form表单中的数据
                emp_form.form("clear");
                //设置title表单名
                emp_dialog.dialog("setTitle", "员工列表");
                //弹出对话框
                emp_dialog.dialog("open");
            },
            editEmp:function () {
                //选中一条需要删除的数据
                var row = emp_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "亲,亲选择一条需要编辑的数据", "error");
                    return;
                }
                //弹出对话框
                //设置title表单名
                emp_dialog.dialog("setTitle", "编辑列表");
                //弹出对话框
                emp_dialog.dialog("open");
                //清空form表单中的数据
                emp_form.form("clear");
                //数据回显
                if (row.dept) {
                    row["dept.id"] = row.dept.id;
                }
                //回显角色的数据
                $.post("/role/listIdsByEmployeeId",{employeeId:row.id},function (data) {

                    combobox_role.combobox("setValues",data);
                });


                emp_form.form("load", row);
            },
            quitEmp:function () {

                var row = emp_datagrid.datagrid("getSelected");
                if (!row) {
                    $.messager.alert("温馨提示", "请选择要离职的员工", "error");
                    return;
                }
                //再次确认是否要离职
                $.messager.confirm("温馨提示", "亲,你确定离职该员工吗?", function (yes) {

                    if (yes) {
                        //发送ajax请求
                        $.get("/employee/quit?id=" + row.id, function (data) {

                            if (data.success) {

                                $.messager.alert("温馨提示", data.message, "info", function () {
                                   cmdObj.reloadEmp();

                                })

                            }else{
                                $.messager.alert("温馨提示",data.message,"error");
                            }
                        });

                    }
                });
            },
            reloadEmp:function () {
                emp_datagrid.datagrid("reload");
            },
            saveEmp:function () {
                var id = $("input[name='id']").val();
                var url;
                if (!id) {
                    url = "/employee/save";
                } else {

                    url = "/employee/edit"
                }
                //发送一条资源去访问后台
                emp_form.form('submit', {
                    url: url,
                    onSubmit:function (param) {
                        //在提交表单之间需要设置角色的id
                        var ids = combobox_role.combobox("getValues");
                        for(var index = 0;index < ids.length ;index++){

                            param["roles["+index+"].id"] = ids[index];
                        }

                    },
                    success: function (data) {
                        console.log(data);
                        data = $.parseJSON(data);

                        if (data.success) {
                            $.messager.alert("温馨提示", data.message, "info", function () {
                               cmdObj.cancelEmp();
                                cmdObj.reloadEmp();
                            });
                        } else {

                            $.messager.alert("温馨提示", data.message, "error")
                        }
                    }
                });
            },
            cancelEmp:function () {
                emp_dialog.dialog("close");
            },
            searchEmp:function () {
                //获取高级查询条件
                var keyword = $("#keyword").val();
                var beginDate = $("#beginDate").val();
                var endDate = $("#endDate").val();

                //再次加载数据
                emp_datagrid.datagrid("load",{keyword:keyword,beginDate:beginDate,endDate:endDate});
            },
            //导出员工的数据
            exportEmp:function () {

                window.location.href="/employee/export"
<<<<<<< HEAD
            },
            addPassword:function () {
                var row =  emp_datagrid.datagrid("getSelected");
                if(!row){
                    $.messager.alert("温馨提示","亲,请选择你的信息","info");
                    return;
                }

                $("#emp_password_dialog").dialog("open");
                $("#xingming").val(row.realName);


            },
            //上班签到
            goWork:function () {
                var row = emp_datagrid.datagrid("getSelected");
                $.post("/employee/goWork",{empId:row.id},function (data) {

                    if(data.success){
                        $.messager.alert("温馨提示",data.message,function () {

                            emp_datagrid.datagrid("reload");
                        })
                    }else {
                        $.messager.alert("温馨提示",data.message,"error");
                    }
                });

            },
            //下班签退
            backWork:function () {
                var row = emp_datagrid.datagrid("getSelected");
                $.post("/employee/backWork",{empId:row.id},function (data) {

                    if(data.success){
                        $.messager.alert("温馨提示",data.message,function () {

                            emp_datagrid.datagrid("reload");
                        })
                    }else {
                        $.messager.alert("温馨提示",data.message,"error");
                    }
                });
            },
            //修改密码
            changePassword:function () {
                $('#password_form').form('submit', {
                    url:"/employee/changePassword",
                    onSubmit: function(param){
                           var row =  $("#emp_datagrid").datagrid("getSelected");
                            param.empId = row.id;
                     },
                     success:function(data){
                        data = $.parseJSON(data);
                            if(data.success){

                                $.messager.alert("温馨提示",data.message,"info",function () {
                                   cmdObj.reloadEmp();
                                    $("#emp_password_dialog").dialog("close");

                                })
                            }else {
                                $.messager.alert("温馨提示",data.message,"error");
                            }
                }
            });
=======
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
            }

        }
        //使用js来绑定事件  使用了属性过滤器来完成该操作
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        //调用方法
        cmdObj[cmd]();
    });


    //end
    //初始化对话框
    $("#emp_dialog").dialog({
        width: 300,
        height: 400,
        closed: true,
        buttons: "#bb"
    })
<<<<<<< HEAD
    //初始化对话框
    $("#emp_password_dialog").dialog({
        title:'员工修改密码',
        width: 200,
        height: 200,
        revealed:true,
        closed: true,
        buttons: "#mm"
    })
=======
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493


})



//时间格式化
function deptFormatter(value, row, index) {

    return value ? value.name : "";

}

//状态格式化回显
function stateFormatter(value, row, index) {

    if (value == 0) {
        return "在职";
    } else if (value == 1) {

        return "<span style='color: red'>" + "离职" + "</span>";
    } else {

        return "待定";
    }

}

//是否为超级管理员回显
function adminFormatter(value, row, index) {
    return value ? "是" : "否";

}